package com.example.mpd.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.mpd.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
@Mapper
public interface UserMapper extends BaseMapper<User>{
    //查询所有用户
    @Select("select * from user")
    public List<User> find();

    //查询所有用户及其订单
    @Select("select * from user")
    @Results(
            {
                    @Result(column = "id", property = "id"),
                    @Result(column = "username", property = "username"),
                    @Result(column = "password", property = "password"),
                    @Result(column = "birthday", property = "birthday"),
                    @Result(column = "id", property = "orders", javaType = List.class,
                            many = @Many(select = "com.example.mpd.mapper.OrderMapper.selectByUid")
                    )
            }
    )
    public List<User> findAllUserAndOrder();

    @Insert("insert into user values (#{id}, #{username}, #{password}, #{birthday})")
    public int add(User user);

    @Delete("delete from user where id=#{id}")
    public int delete(int id);

    @Select("select * from user where id=#{id}")
    public User findById(int id);

    @Update("update user set username=#{username}, password=#{password}, birthday=#{birthday} where id=#{id}")
    public int update(User user);
}
